package com.wuliang.generate.common.core.response;

import com.wuliang.generate.common.core.utils.JsonUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.nio.charset.StandardCharsets;

/**
 * @param <T>泛型
 * @author baiyl
 * @className Result<T>
 * @description 通用的接口返回数据类型
 */
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
public class Result<T> {

    /**
     * 提示信息
     */
    private String message;

    /**
     * 返回的数据内容
     */
    private T value;

    /**
     * 自定义状态码
     */
    private Integer returnCode;

    /**
     * 无消息返回
     *
     * @param returnCode {@link ReturnCode}
     */
    public Result(ReturnCode returnCode) {
        this.message = returnCode.getMessage();
        this.returnCode = returnCode.getCode();
    }

    /**
     * @param value 返回内容
     * @param returnCode {@link ReturnCode}
     */
    public Result(T value, ReturnCode returnCode) {
        this.value = value;
        this.message = returnCode.getMessage();
        this.returnCode = returnCode.getCode();
    }

    /**
     * @param message 自定义提示信息
     * @param returnCode {@link ReturnCode}
     */
    public Result(String message, ReturnCode returnCode) {
        this.message = message;
        this.returnCode = returnCode.getCode();
    }

    @Override
    public String toString() {
        return JsonUtils.toJson(this);
    }

    public byte[] toBytes() {
        return toString().getBytes(StandardCharsets.UTF_8);
    }
}
